﻿using System;
using System.Data;
using System.Globalization;
using Model;
using Service;


public partial class ActionTakenAddNew : System.Web.UI.Page
{
    private DirectServiceActionTaken _DirectServerActionTaken = new DirectServiceActionTaken();
    private ReferenceDataServiceActionTaken _ReferenceDataServiceActionTaken = new ReferenceDataServiceActionTaken();
    private int planningId;
    private int actionTakenId;
    protected void Page_Load(object sender, EventArgs e)
    {
        // planningId = GetPlanningId();
        actionTakenId = GetActionTakenId();
        if (!IsPostBack)
        {
            InitValueToFormLoad();
        }

    }
    private void InitValueToFormLoad()
    {
        DataTable tableEdit = new DataTable();
        tableEdit = _DirectServerActionTaken.GetEditActionTaken(actionTakenId);
        if (tableEdit.Rows.Count != 0)
        {
            txtWorkorderId.Text = tableEdit.Rows[0]["workOrderID"].ToString();
            txtTaskID.Text = tableEdit.Rows[0]["taskInWorkOrderID"].ToString();
            txtPlanningID.Text = tableEdit.Rows[0]["PLANNINGID"].ToString();
            txtEquipmentID.Text = tableEdit.Rows[0]["CODENUMBER"].ToString();


     
            txtStartDate.Text = tableEdit.Rows[0]["startDate"].ToString().Split(' ')[0];
            txtEndDate.Text = tableEdit.Rows[0]["endDate"].ToString().Split(' ')[0]; 
            txtTotaltime.Text = tableEdit.Rows[0]["durationInMinut"].ToString();
            txtCostPerRepairation.Text = tableEdit.Rows[0]["CostPerReparation"].ToString();
            txtDescription.Text = tableEdit.Rows[0]["description"].ToString();

            drlTaskStatus.SelectedValue = (tableEdit.Rows[0]["TaskStatusID"].ToString());
            string demaged = tableEdit.Rows[0]["DEMAGED"].ToString();
            if (demaged == "True")
            {
                this.chbDemage.Checked = true;
                this.chbDemage.Text = "This equipment is demaged !";
            }
            else this.chbDemage.Checked = false;


        }
    }

   
    public int GetActionTakenId()
    {
        return !String.IsNullOrEmpty(Request.QueryString["actionTakenId"]) ? Int32.Parse(Request.QueryString["actionTakenId"]) : 0;
    }

    public int GetPageIndex()
    {
        return Session["pageIndex"] != null
                   ?
                       Int32.Parse(Session["pageIndex"].ToString())
                   : 0;
    }
    public int GetTabIndex()
    {
        int tabIndex = Request.QueryString["tabIndex"] != null
                        ?
                            Int32.Parse(Request.QueryString["tabIndex"])
                        : 0;
        if (Session["defaultTabIndex"] != null)
        {
            tabIndex = Int32.Parse(Session["defaultTabIndex"].ToString());
            Session["defaultTabIndex"] = null;
        }

        return tabIndex;
    }

    protected void btnAddNewStatus_Click(object sender, EventArgs e)
    {
        TaskStatus taskstatus = new TaskStatus();
        if (txtNewStatus.Text != "")
        {
            taskstatus.TaskStatusId = 0;
            taskstatus.TaskStatusLabel = txtNewStatus.Text;
            _ReferenceDataServiceActionTaken.SaveTaskStatus(taskstatus);
            
          
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

        insertActionTaken();
      
    }
    private void insertActionTaken()
    {
        Reparation reparation = new Reparation();
        reparation.ReparationId = 0;
        reparation.PlanningId = int.Parse(txtPlanningID.Text);
       
        reparation.TaskStatusId = int.Parse(txtTaskID.Text);
        reparation.StartDate = DateTime.Parse(txtStartDate.Text, new CultureInfo("en-US", true));
        reparation.EndDate = DateTime.Parse(txtEndDate.Text, new CultureInfo("en-US", true));
        reparation.DurationInMinut = int.Parse(txtTotaltime.Text);
        reparation.CostPerReparation = float.Parse(txtCostPerRepairation.Text);
        _ReferenceDataServiceActionTaken.SaveActionTaken(reparation);
         
    }
    protected void btnAddSparePart_Click(object sender, EventArgs e)
    {

       
       // Response.Redirect("<javascript:OpenPopupWindow('worOrderRequestIntranet.aspx')");


        //System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>");
        //System.Web.HttpContext.Current.Response.Write("<a href="javascript:OpenPopupWindow('workOrderAddAdmin.aspx')"></a>");
        //System.Web.HttpContext.Current.Response.Write("</SCRIPT>");
       // Response.Write()

        //Response.Write("<tr>");
        //Response.Write("<td><a href="javascript:OpenPopupWindow('workOrderAddAdmin.aspx')" 
        //    + ">");
       
        //Response.Write("</td>");
        
        //Response.Write("</tr>");

    }
    

}
